<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['empId']"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="工号">
        <a-input placeholder="工号" v-decorator="['empCode',{rules: [{required: true, message: '请输入工号'}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="员工姓名">
        <a-input placeholder="员工姓名" v-decorator="['empName',{rules: [{required: true, message: '请输入员工姓名'}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="部门">
        <a-input placeholder="部门" v-decorator="['departId',{rules: [{required: true, message: '请输入部门'}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="级别">
        <a-select  style="width: 100%"  v-decorator="['rankId', {rules: [{ required: true, message: '请选择级别' }]}]" :allowClear="true">
          <a-select-option v-for="(d,index) in rankList_TableDict" :key="index" :value="d.key" >{{ d.value }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="人员状态">
        <a-radio-group v-decorator="['status', {rules: [{ required: true, message: '请选择人员状态' }]}]">
          <a-radio v-for="(d,index) in project_user_statusDictList" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="城市">
        <a-input placeholder="城市" v-decorator="['city',{rules: [{required: true, message: '请输入城市'}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="电子邮箱">
        <a-input placeholder="电子邮箱" v-decorator="['email']"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="电话">
        <a-input placeholder="电话" v-decorator="['phone']"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="职位">
        <a-input placeholder="职位" v-decorator="['jobRole']"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import moment from "moment";
import { dictList,getDictMapKey,tableDictList,getTableDictKey } from '@/utils/dict'
import { saveEmployee,getEmployeeInfo } from '@/api/project/employee'
import pick from 'lodash.pick'

export default {
  name: 'EmployeeModal',
  props: {
  },
  components: {
  },
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      rankList:[],
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {
  },
  created () {
    this.dictSync();
  },
  methods: {
    async dictSync(){
      this.rankList_TableDict=await tableDictList('rankList');
      this.project_user_statusDictList=await dictList('project_user_status');
    } ,
    add () {
      this.form.resetFields()
      this.edit()
    },
    edit (empId) {
      if(empId){
        getEmployeeInfo(empId).then(res => {
          this.mdl = Object.assign(res)
          this.visible = true
          ;
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.mdl,'empId','empCode','empName','departId','rankId','status','city','email','phone','jobRole'))
          })
        })
      }else{
        this.visible = true
        this.mdl={}
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl,'empCode','empName','departId','rankId','status','city','email','phone','jobRole'))
        })
      }
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveEmployee(values).then(res => {
            if (res.code === 0) {
              this.$message.success('保存成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.success(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
